home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12726 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Rounding a double
  5. Date: Tue, 02 Apr 96 12:49:44 GMT
  6. Organization: none
  7. Message-ID: <828449384snz@genesis.demon.co.uk>
  8. References: <4jprh2$1li@newsgate.dircon.co.uk> <4jq5u5INN80u@mayne.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jq5u5INN80u@mayne.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >In article <4jprh2$1li@newsgate.dircon.co.uk>,
  18. >Peter Smith <pg-smith@dircon.co.uk> wrote:
  19. > >Has anybody got a C function to round a double to a number of decimal 
  20. > >places that:
  21. > >a) is readable
  22. > >b) is relatively short
  23. > >c) works !!
  24. >
  25. >double round(double x, int digits)
  26. >
  27. >{
  28. >        char buffer[20];
  29. >        sprintf(buffer,"%8.8f",x);
  30. >
  31. >        buffer >>= (8 - digits);
  32. >
  33. >        return atof(buffer);
  34. >}
  35.  
  36. There was a requirement here that it works! :-)
  37.  
  38. -- 
  39. -----------------------------------------
  40. Lawrence Kirby | fred@genesis.demon.co.uk
  41. Wilts, England | 70734.126@compuserve.com
  42. -----------------------------------------
  43.